package com.yy.yycompnent.slide
{
	import com.yy.base.App;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 *滑动条 控制器
	 * @author YY
	 * 
	 */
	public class Slide extends EventDispatcher
	{
		protected var _handle:SliderBar;			//滚动滑块
		protected var _back:DisplayObject;				//滚动槽
		protected var _orientation:String;//滚动方向
		protected var _tick:Number = 0.01;//
		private var _value:Number = 0;//当前的值
		private var _max:Number = 100;//最大值
		private var _min:Number = 0;//最小值
		
		public var _backClick:Boolean = true;//是否可以点击点击滚动槽
		
		public static const HORIZONTAL:String = "horizontal";
		public static const VERTICAL:String = "vertical";
		/**
		 * 
		 * @param view
		 * 	view.back
		 * 	view.handle
		 * 
		 * @param orientation 滚动方向
		 * @param defaultHandler 回调函数
		 * 
		 */
		public function Slide(back:DisplayObject, handle:SliderBar, orientation:String = VERTICAL, defaultHandler:Function = null)
		{
			_orientation = orientation;
			_back = back;
			_handle = handle;
			if(Boolean(defaultHandler)) {
				addEventListener(Event.CHANGE, defaultHandler);
			}
			init();
		}
		
		/**
		 * 初始化组件
		 */
		protected function init():void
		{
			addEvent();
			draw();
		}
		
		private function  addEvent():void {
			_handle.sliderBar.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
		}
		private function  removeEvent():void {
			_handle.sliderBar.removeEventListener(MouseEvent.MOUSE_DOWN, onDrag);
		}
		
		
		/**
		 * Draws the back of the slider.
		 */
		protected function drawBack():void
		{
			if(_backClick)
			{
				_back.addEventListener(MouseEvent.MOUSE_DOWN, onBackClick);
			}
			else
			{
				_back.removeEventListener(MouseEvent.MOUSE_DOWN, onBackClick);
			}
		}
		
		/**
		 * Draws the handle of the slider.
		 */
		protected function drawHandle():void
		{	
			positionHandle();
		}
		
		/**
		 * Adjusts value to be within minimum and maximum.
		 */
		protected function correctValue():void
		{
			if(_max > _min)
			{
				_value = Math.min(_value, _max);
				_value = Math.max(_value, _min);
			}
			else
			{
				_value = Math.max(_value, _max);
				_value = Math.min(_value, _min);
			}
		}
		
		/**
		 * Adjusts position of handle when value, maximum or minimum have changed.
		 * TODO: Should also be called when slider is resized.
		 */
		protected function positionHandle():void
		{
			var range:Number;
			if(_orientation == HORIZONTAL)
			{
				range = _back.width - _handle.width;
				_handle.x = (_value - _min) / (_max - _min) * range;
			}
			else
			{
				range = _back.height - _handle.height;
				_handle.y = (_value - _min) / (_max - _min) * range;
			}
		}
		
		
		
		
		///////////////////////////////////
		// public methods
		///////////////////////////////////
		
		/**
		 * Draws the visual ui of the component.
		 */
		public function draw():void
		{
			drawBack();
			drawHandle();
		}
		
		/**
		 * Convenience method to set the three main parameters in one shot.
		 * @param min The minimum value of the slider.
		 * @param max The maximum value of the slider.
		 * @param value The value of the slider.
		 */
		public function setSliderParams(min:Number, max:Number, value:Number):void
		{
			this.minimum = min;
			this.maximum = max;
			this.value = value;
		}
		
		
		
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////
		
		/**
		 *点击滑动槽 
		 * @param event
		 * 
		 */
		protected function onBackClick(event:MouseEvent):void
		{
			var range:Number;
			if(_orientation == HORIZONTAL)
			{
				range = _back.width - _handle.width;
				var mouseX:Number = event.localX;
				_handle.x = mouseX - _handle.width / 2;
				_handle.x = Math.max(_handle.x, 0);
				_handle.x = Math.min(_handle.x, range);
				_value = _handle.x / range * (_max - _min) + _min;
			}
			else
			{
				range = _back.height - _handle.height;
				var mouseY:Number = event.localY;
				_handle.y = mouseY - _handle.height / 2;
				_handle.y = Math.max(_handle.y, 0);
				_handle.y = Math.min(_handle.y, range);
				_value = _handle.y / (range) * (_max - _min) + _min;
			}
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		/**
		 * Internal mouseDown handler. Starts dragging the handle.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onDrag(event:MouseEvent):void
		{
			App.appStage.addEventListener(MouseEvent.MOUSE_UP, onDrop);
			App.appStage.addEventListener(MouseEvent.MOUSE_MOVE, onSlide);
			var range:Number;
			if(_orientation == HORIZONTAL)
			{
				range = _back.width - _handle.width;
				_handle.sliderBar.startDrag(false, new Rectangle(0, 0, range, 0));
			}
			else
			{
				range = _back.height - _handle.height;
				trace("dragRange:" + range);
				_handle.sliderBar.startDrag(false, new Rectangle(0, 0, 0, range));
			}
		}
		
		/**
		 * Internal mouseUp handler. Stops dragging the handle.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onDrop(event:MouseEvent):void
		{
			App.appStage.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
			App.appStage.removeEventListener(MouseEvent.MOUSE_MOVE, onSlide);
			_handle.sliderBar.stopDrag();
		}
		
		/**
		 * Internal mouseMove handler for when the handle is being moved.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onSlide(event:MouseEvent):void
		{
			var oldValue:Number = _value;
			var range:Number = 0;
			if(_orientation == HORIZONTAL)
			{
				range = _back.width - _handle.width;
				_value = _handle.x / (range) * (_max - _min) + _min;
			}
			else
			{
				range = int(_back.height - _handle.height);//转成整数是因为滑动的时候flash舍去了小数点
				_handle.y = _handle.y;
				trace("range:" + range);
				trace("handle_y:" + Number(_handle.y));
				_value = _handle.y / (range) * (_max - _min) + _min;
			}
			if(_value != oldValue)
			{
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		
		
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		/**
		 * Sets / gets whether or not a click on the background of the slider will move the handler to that position.
		 */
		public function set backClick(b:Boolean):void
		{
			_backClick = b;
			draw();
		}
		public function get backClick():Boolean
		{
			return _backClick;
		}
		
		/**
		 * Sets / gets the current value of this slider.
		 */
		public function set value(v:Number):void
		{
			_value = v;
			correctValue();
			draw();
			dispatchEvent(new Event(Event.CHANGE));
		}
		public function get value():Number
		{
			return Math.round(_value / _tick) * _tick;
		}
		
		/**
		 * Gets the value of the slider without rounding it per the tick value.
		 */
		public function get rawValue():Number
		{
			return _value;
		}
		
		/**
		 * Gets / sets the maximum value of this slider.
		 */
		public function set maximum(m:Number):void
		{
			_max = m;
			correctValue();
			draw();
		}
		public function get maximum():Number
		{
			return _max;
		}
		
		/**
		 * Gets / sets the minimum value of this slider.
		 */
		public function set minimum(m:Number):void
		{
			_min = m;
			correctValue();
			draw();
		}
		public function get minimum():Number
		{
			return _min;
		}
		
		/**
		 * Gets / sets the tick value of this slider. This round the value to the nearest multiple of this number. 
		 */
		public function set tick(t:Number):void
		{
			_tick = t;
		}
		public function get tick():Number
		{
			return _tick;
		}
	}
}